Ext.define('App.authorization.user.view.UserEditor', {
    extend: 'Extend.window.FormWindow',
    alias: 'widget.UserEditor',
    config: {
        entity: 'entity',
        window: {
            title: '用户信息编辑',
            width: 1024,
            height: 520
        }
    },
    defaults: {columnWidth: .33},
    items: [{
        xtype: 'hidden',
        name: 'id'
    },{
        xtype: 'hidden',
        name: 'cmp'
    }, {
        xtype: 'textfield',
        name: 'username',
        fieldLabel: '帐号',
        allowBlank: false,
        blankText: '帐号为必填字段，不能为空！！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        maxLength: 32,
        validator: function (val) {
            return this.up('UserEditor').checkUsername(val);
        }
    }, {
        xtype: 'textfield',
        name: 'password',
        fieldLabel: '密码',
        allowBlank: false,
        blankText: '密码为必填字段，不能为空！！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        maxLength: 32,
        inputType: 'password'
    }, {
        xtype: 'textfield',
        name: 'name',
        fieldLabel: '姓名',
        maxLength: 32
    }, {
        xtype: 'textfield',
        name: 'chinasoftNumber',
        fieldLabel: '中软工号',
        minLength: 10,
        maxLength: 10,
        allowBlank: false,
        blankText: '中软工号为必填字段，不能为空！！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        regex: /^\d+$/,
        regexText: '中软工号错误，中软工号为10位数字组成！'
    }, {
        xtype: 'textfield',
        name: 'email',
        fieldLabel: '邮箱',
        maxLength: 128,
        vtype: 'email'
    }, {
        xtype: 'booleancombobox',
        name: 'locked',
        fieldLabel: '帐号锁定',
        allowBlank: false,
        blankText: '帐号锁定为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        xtype: 'datefield',
        name: 'accountExpiringDate',
        format: 'Y-m-d',
        editable: false,
        fieldLabel: '帐号失效时间',
        allowBlank: false,
        blankText: '帐号失效时间为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        xtype: 'datefield',
        format: 'Y-m-d',
        editable: false,
        name: 'credentialsExpiringDate',
        fieldLabel: '密码失效时间',
        allowBlank: false,
        blankText: '密码失效时间为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        xtype: 'booleancombobox',
        name: 'systemuser',
        fieldLabel: '系统用户',
        allowBlank: false,
        blankText: '系统用户字为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        readOnly: true
    }, {
        name: 'roles',
        xtype: 'gridfield',
        fieldLabel: '角色配置',
        columnWidth: 1,
        height: 325,
        readOnly: false,
        submitFields: ['id', 'name'],
        addHandler: function () {
            var grid = this;
            Ext.create('Extend.window.SelectionWindow', {
                height: 500,
                grid: 'App.authorization.role.view.RoleSelection',
                listeners: {
                    selected: function (rs) {
                        grid.loadRecords(rs, true);
                    }
                }
            }).show();
        },
        columns: [{
            header: '角色名',
            dataIndex: 'name'
        }, {
            header: '描述',
            dataIndex: 'rolename'
        }, {
            header: '创建人',
            dataIndex: 'creator'
        }, {
            xtype: 'cdtcolumn',
            header: '创建时间',
            dataIndex: 'cdt'
        }]
    }],
    listeners: {
        afterrender: function () {
            var me = this;
            if (me.isImport == true) {
                me.down('[name="username"]').setReadOnly(true);
                me.down('booleancombobox[name="systemuser"]').setReadOnly(true)
            } else {
                me.down('booleancombobox[name="systemuser"]').setReadOnly(false)
            }
        },
        load: function ($this, action) {
            var me = this;
            var data = action.result.data;
            if (!Ext.isEmpty(data.id)) {
                me.down('[name="username"]').setReadOnly(true);
            }
        }
    },
    checkUsername: function (username) {
        var me = this;
        var isSystemuser = me.down('booleancombobox[name="systemuser"]').getValue();
        if (this.isImport != true && isSystemuser == false) {
            var flag = false;
            Ext.Ajax.request({
                url: 'hrm/employee/exists',
                params: {'username': me.down('textfield[name="username"]').getValue()},
                async: false,
                success: function (resp) {
                    flag = resp.result.data;
                }
            });
            if (flag == false) {
                return "添加非系统用户，用户名为华为工号并且必须在人力管理中存在！";
            }
            return flag;
        }
        return true;
    }
});
